const { ipcMain } = require("electron/main");
const { app } = require("electron");
const getToken = require("../utils/getToken");
const log = require("../utils/log");

// 获取token和port
ipcMain.handle("getTokenAndPort-ipc", async () => {
  return await getToken();
});

// 写入日志

ipcMain.handle("setLog-ipc", async (e, value) => {
  log.info(value);
});

const fs = require("fs");

const { path_config_config_json, path_config } = require("../constant/index");

// 检查配置文件是否存在
const path_config_fn = require("../utils/path");

ipcMain.handle("getConfig-ipc", (e, key) => {
  path_config_fn();

  let data = fs.readFileSync(path_config_config_json);

  data = JSON.parse(data);
  return data[key];
});

ipcMain.handle("setConfig-ipc", (e, key, value) => {
  path_config_fn();
  let data = fs.readFileSync(path_config_config_json);

  data = JSON.parse(data);

  // 开机启动
  if (key === "bootUp") {
    app.setLoginItemSettings({
      openAtLogin: value,
    });
  }

  data = { ...data, [key]: value };
  fs.writeFileSync(path_config_config_json, JSON.stringify(data));

  return true;
});
